package com.ship.dispatch.service.fob.impl;

import com.alibaba.fastjson2.JSONObject;
import com.ship.common.core.utils.HttpRequestUtil;
import com.ship.dispatch.bean.fob.*;
import com.ship.dispatch.bpm.utils.JsonUtils;
import com.ship.dispatch.service.fob.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 异步调用解析ocr
 */
@Service
@Slf4j
@EnableAsync
public class AsyncService {

    @Lazy
    @Resource
    SpFobSyzlzzwjService spFobSyzlzzwjService;

    @Lazy
    @Resource
    SpFobSyzlxzwjService spFobSyzlxzwjService;
    @Lazy
    @Resource
    SpFobDesQrjlbgService spFobDesQrjlbgService;
    @Lazy
    @Resource
    SpFobReturnSalesJlbgService spFobReturnSalesJlbgService;

    @Lazy
    @Resource
    SpFobSalesSyzlzzwjService spFobSalesSyzlzzwjService;
    @Resource
    SpFobFileRecordService spFobFileRecordService;

    @Resource
    HttpRequestUtil httpRequestUtil;
    @Async
    public void executeSyzlzzwj(Long id,String fileId){
        SpFobFileRecord fileRecord = spFobFileRecordService.getById(fileId);
        if(fileRecord!=null){
            try{
                URL url = new URL(fileRecord.getPath());
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setConnectTimeout(5000);
                connection.setReadTimeout(5000);
                InputStream in = connection.getInputStream();
                if(in!=null){
                    JSONObject infos = httpRequestUtil.ocrJLReport(in,fileRecord.getId()+"");
                    SpFobSyzlzzwj spFobSyzlzzwj = JsonUtils.jsonToObject(JsonUtils.objectToJson(infos),SpFobSyzlzzwj.class);
                    spFobSyzlzzwj.setId(id);
                    spFobSyzlzzwj.setField3("1");
                    spFobSyzlzzwjService.updateById(spFobSyzlzzwj);
                }
            }catch (Exception e){

                SpFobSyzlzzwj spFobSyzlzzwj = new SpFobSyzlzzwj();
                // 识别失败
                spFobSyzlzzwj.setField3("2");
                spFobSyzlzzwjService.updateById(spFobSyzlzzwj);
            }
        }
    }
    @Async
    public void executeSyzlxzwj(Long id,String fileId){
        try{
            SpFobFileRecord fileRecord = spFobFileRecordService.getById(fileId);
            if(fileRecord!=null){
                URL url = new URL(fileRecord.getPath());
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setConnectTimeout(5000);
                connection.setReadTimeout(5000);
                InputStream in = connection.getInputStream();
                if(in!=null){
                    JSONObject infos = httpRequestUtil.ocrJLReport(in,fileId);
                    SpFobSyzlxzwj spFobSyzlzzwj = JsonUtils.jsonToObject(JsonUtils.objectToJson(infos),SpFobSyzlxzwj.class);
                    spFobSyzlzzwj.setId(id);
                    spFobSyzlzzwj.setField3("1");
                    spFobSyzlxzwjService.updateById(spFobSyzlzzwj);
                }
            }
        }catch (Exception e){
            SpFobSyzlxzwj spFobSyzlzzwj = new SpFobSyzlxzwj();
            spFobSyzlzzwj.setId(id);
            spFobSyzlzzwj.setField3("2");
            spFobSyzlxzwjService.updateById(spFobSyzlzzwj);
        }
    }
    @Async
    public void executDesQrjlbg(Long id,String fileId){
        try{
            SpFobFileRecord fileRecord = spFobFileRecordService.getById(fileId);
            if(fileRecord!=null){
                URL url = new URL(fileRecord.getPath());
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setConnectTimeout(5000);
                connection.setReadTimeout(5000);
                InputStream in = connection.getInputStream();
                if(in!=null){
                    JSONObject infos = httpRequestUtil.ocrJLReport(in,fileId);
                    SpFobDesQrjlbg spFobSyzlzzwj = JsonUtils.jsonToObject(JsonUtils.objectToJson(infos),SpFobDesQrjlbg.class);
                    spFobSyzlzzwj.setId(id);
                    spFobSyzlzzwj.setField3("1");
                    spFobDesQrjlbgService.updateById(spFobSyzlzzwj);
                }
            }
        }catch (Exception e){
            SpFobDesQrjlbg spFobSyzlzzwj = new SpFobDesQrjlbg();
            spFobSyzlzzwj.setId(id);
            spFobSyzlzzwj.setField3("2");
            spFobDesQrjlbgService.updateById(spFobSyzlzzwj);
        }
    }
    @Async
    public void executReturnSalesJlbg(Long id,String fileId){
        try{
            SpFobFileRecord fileRecord = spFobFileRecordService.getById(fileId);
            if(fileRecord!=null){
                URL url = new URL(fileRecord.getPath());
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setConnectTimeout(5000);
                connection.setReadTimeout(5000);
                InputStream in = connection.getInputStream();
                if(in!=null){
                    JSONObject infos = httpRequestUtil.ocrJLReport(in,fileId);
                    SpFobReturnSalesJlbg spFobSyzlzzwj = JsonUtils.jsonToObject(JsonUtils.objectToJson(infos),SpFobReturnSalesJlbg.class);
                    spFobSyzlzzwj.setId(id);
                    spFobSyzlzzwj.setField3("1");
                    spFobReturnSalesJlbgService.updateById(spFobSyzlzzwj);
                }
            }
        }catch (Exception e){
            SpFobReturnSalesJlbg spFobSyzlzzwj = new SpFobReturnSalesJlbg();
            spFobSyzlzzwj.setId(id);
            spFobSyzlzzwj.setField3("2");
            spFobReturnSalesJlbgService.updateById(spFobSyzlzzwj);
        }
    }
    @Async
    public void executSalesSyzlzzwj(Long id,String fileId){
        try{
            SpFobFileRecord fileRecord = spFobFileRecordService.getById(fileId);
            if(fileRecord!=null){
                URL url = new URL(fileRecord.getPath());
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setConnectTimeout(5000);
                connection.setReadTimeout(5000);
                InputStream in = connection.getInputStream();
                if(in!=null){
                    JSONObject infos = httpRequestUtil.ocrJLReport(in,fileId);
                    SpFobSalesSyzlzzwj spFobSyzlzzwj = JsonUtils.jsonToObject(JsonUtils.objectToJson(infos),SpFobSalesSyzlzzwj.class);
                    spFobSyzlzzwj.setId(id);
                    spFobSyzlzzwj.setField3("1");
                    spFobSalesSyzlzzwjService.updateById(spFobSyzlzzwj);
                }
            }
        }catch (Exception e){
            SpFobSalesSyzlzzwj spFobSyzlzzwj = new SpFobSalesSyzlzzwj();
            spFobSyzlzzwj.setId(id);
            spFobSyzlzzwj.setField3("2");
            spFobSalesSyzlzzwjService.updateById(spFobSyzlzzwj);
        }
    }

}
